home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / GrayFilter.java < prev    next >
Text File  |  1998-06-30  |  3KB  |  78 lines

  1. /*
  2.  * @(#)GrayFilter.java    1.6 98/01/30
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing;
  21.  
  22. import java.awt.*;
  23. import java.awt.image.*;
  24.  
  25. /**
  26.  * An image filter that "disables" an image by turning
  27.  * it into a grayscale image, and brightening the pixels
  28.  * in the image.
  29.  *
  30.  * @author      Jeff Dinkins
  31.  * @author      Tom Ball
  32.  * @author      Jim Graham
  33.  * @version     1.6 01/30/98
  34.  */
  35. public class GrayFilter extends RGBImageFilter {
  36.     private boolean brighter;
  37.     private int percent;
  38.     
  39.     /**
  40.      * Creates a disabled image
  41.      */
  42.     public static Image createDisabledImage (Image i) {
  43.     GrayFilter filter = new GrayFilter(true, 50);
  44.     ImageProducer prod = new FilteredImageSource(i.getSource(), filter);
  45.     Image grayImage = Toolkit.getDefaultToolkit().createImage(prod);
  46.     return grayImage;
  47.     }
  48.     
  49.     public GrayFilter(boolean b, int p) {
  50.         brighter = b;
  51.         percent = p;
  52.  
  53.     // canFilterIndexColorModel indicates whether or not it is acceptable
  54.     // to apply the color filtering of the filterRGB method to the color
  55.     // table entries of an IndexColorModel object in lieu of pixel by pixel
  56.          // filtering.
  57.         canFilterIndexColorModel = true;
  58.     }
  59.     
  60.     public int filterRGB(int x, int y, int rgb) {
  61.         // Use NTSC conversion formula.
  62.     int gray = (int)((0.30 * ((rgb >> 16) & 0xff) + 
  63.                          0.59 * ((rgb >> 8) & 0xff) + 
  64.                          0.11 * (rgb & 0xff)) / 3);
  65.     
  66.         if (brighter) {
  67.             gray = (255 - ((255 - gray) * (100 - percent) / 100));
  68.         } else {
  69.             gray = (gray * (100 - percent) / 100);
  70.         }
  71.     
  72.         if (gray < 0) gray = 0;
  73.         if (gray > 255) gray = 255;
  74.         return (rgb & 0xff000000) | (gray << 16) | (gray << 8) | (gray << 0);
  75.     }
  76. }
  77.  
  78.